package com.chidopi.market.bookstore.service.impl;

import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.chidopi.cdf.dao.IBaseDao;
import com.chidopi.cdf.exception.CdfException;
import com.chidopi.cdf.service.IBaseService;
import com.chidopi.market.bookstore.domain.ProductID2Book;
import com.chidopi.market.bookstore.domain.Subscribe4iOS;


public class Subscribe4iOSServiceImpl implements IBaseService<Subscribe4iOS> {
	
	private static Logger logger = LoggerFactory.getLogger(Subscribe4iOSServiceImpl.class);
	
	@Autowired
	@Qualifier("subscribe4iOSDao")
	private IBaseDao<Subscribe4iOS> baseDao;
	
	@Autowired
	@Qualifier("productID2BookDao")
	private IBaseDao<ProductID2Book> productDao;
	
	@Override
	public int insert(Subscribe4iOS t) throws Exception {
		return this.baseDao.insert(t);
	}

	@Override
	public int update(Subscribe4iOS t) throws Exception {
		return this.baseDao.update(t);
	}

	@Override
	public int delete(Subscribe4iOS t) throws Exception {
		Integer id = t.getId();
		List<ProductID2Book> products = this.productDao.find(" a.PRODUCT_TYPE = 2 AND a.BOOK_ID = ?", id);
		if(products != null && products.size() > 0){
			logger.debug("delete [" + id + "] failure: ProductID2Book.size()=" + products.size());
			throw new CdfException("error.subscribe4ios.delete.productid2book");
		}
		return this.baseDao.delete(t);
	}

	@Override
	public List<Subscribe4iOS> find(String sqlWhere, Object... args) throws Exception {
		logger.debug(sqlWhere);
		logger.debug(Arrays.asList(args).toString());
		return this.baseDao.find(sqlWhere, args);
	}

	@Override
	public Subscribe4iOS findById(Object id) throws Exception {
		Subscribe4iOS ios = this.baseDao.findById(id);
		
		return ios;
	}

	@Override
	public int getNextSequence() throws Exception {
		return 0;
	}

}
